package com.rchat.pocmini.activity.register;

import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.rchat.pocmini.R;
import com.rchat.pocmini.bean.CheckAccountBean;
import com.rchat.pocmini.bean.RegisterBean;
import com.rchat.pocmini.bean.VerificationCodeBean;
import com.rchat.pocmini.global.AppGlobal;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.UrlsGlobal;
import com.rchat.pocmini.util.PhoneUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by ljd on 2017/7/27.
 */

public class RegisterPresenterImpl extends RegisterContract.Presenter {


    private boolean isRegister = false;


    @Override
    void checkAccount(String account) {
        if (TextUtils.isEmpty(account)) {
            //账号为空不去检测
            return;
        }
        mModel.checkAccount(account, new HttpCallbackImpl<CheckAccountBean>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(CheckAccountBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(R.string.register_account_can_register);
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                switch (bean.getCode()) {
                    case HttpCodeGlobal.RES_CODE_423 + "":
                        mView.showShortToast(R.string.register_account_has_registered_error);
                        break;
                    case HttpCodeGlobal.RES_CODE_303 + "":
                        mView.showShortToast("当前账号不可用，请重新选择");
                        break;
                    default:
                        mView.showNetError(bean.getCode(), bean.getDesc());
                }
            }

            @Override
            public void onFinish() {

            }
        });
    }

    @Override
    void getVerificationCode(String userName, String nickName, String pwd, String pwdConfirm, String phone) {
        if (nickName.equals("")) {
            mView.showShortToast(R.string.register_nick_name_empty_error);
            return;
        }
        if (nickName.length() > 10) {
            mView.showShortToast(R.string.register_nick_name_more_then_ten_error);
            return;
        }
        if (nickName.equals("")) {
            mView.showShortToast(R.string.register_account_empty_error);
            return;
        }

        if (isChinese(userName)) {
            mView.showShortToast(R.string.register_account_is_chinese_error);
            return;
        }

        if (pwd.equals("")) {
            mView.showShortToast(R.string.register_password_empty_error);
            return;
        }
        if (pwdConfirm.length() < 6) {
            mView.showShortToast(R.string.register_password_less_then_six_error);
            return;
        }
        if (pwdConfirm.equals("")) {
            mView.showShortToast(R.string.register_password_less_then_six_error);
            return;
        }

        if (!PhoneUtil.isMobileNO(phone)) {
            mView.showShortToast(R.string.register_phone_number_format_error);
            return;
        }
        if (!pwd.equals(pwdConfirm)) {
            mView.showShortToast(R.string.register_password_enter_twice_no_same);
            return;
        }

        mModel.getVerificationCode(phone, new HttpCallbackImpl<VerificationCodeBean>() {
            @Override
            public void onStart() {
                mView.getVerificationCodeBegin();
            }

            @Override
            public void onSuccess(VerificationCodeBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(R.string.register_verification_code_send);
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
            }

            @Override
            public void onFinish() {

            }
        });
    }


    @Override
    void register(String nickName, String userName, String pwd, String pwdConfirm, String verificationCode, String mail, String phone) {
        if (isRegister) {
            return;
        }
        if (nickName.equals("")) {
            mView.showShortToast(R.string.register_nick_name_empty_error);
            return;
        }
        if (nickName.length() > 10) {
            mView.showShortToast(R.string.register_nick_name_more_then_ten_error);
            return;
        }
        if (userName.equals("")) {
            mView.showShortToast(R.string.register_account_empty_error);
            return;
        }

        if (isChinese(userName)) {
            mView.showShortToast(R.string.register_account_is_chinese_error);
            return;
        }

        if (pwd.equals("")) {
            mView.showShortToast(R.string.register_password_empty_error);
            return;
        }
        if (pwdConfirm.length() < 6) {
            mView.showShortToast(R.string.register_password_less_then_six_error);
            return;
        }
        if (pwdConfirm.equals("")) {
            mView.showShortToast(R.string.register_password_less_then_six_error);
            return;
        }

        if (!mail.equals("")) {
            if (!PhoneUtil.isEmail(mail)) {
                mView.showShortToast(R.string.register_mail_format_error);
                return;
            }
        }
        if (!PhoneUtil.isMobileNO(phone)) {
            mView.showShortToast(R.string.register_phone_number_format_error);
            return;
        }
        if (!pwd.equals(pwdConfirm)) {
            mView.showShortToast(R.string.register_password_enter_twice_no_same);
            return;
        }
        isRegister = true;

        mModel.register(nickName, userName, pwd, pwdConfirm, verificationCode, mail, phone,
                new HttpCallbackImpl<RegisterBean>() {
                    @Override
                    public void onStart() {
                        mView.setProcessBarVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onSuccess(RegisterBean bean) {
                        if (mView == null) {
                            return;
                        }
                        isRegister = false;
                        mView.showShortToast(bean.getDesc());
                        mActivity.finish();
                    }

                    @Override
                    public void onFailureResult(BaseBean bean) {
                        if (mView == null) {
                            return;
                        }
                        isRegister = false;
                        switch (bean.getCode()) {
                            case HttpCodeGlobal.RES_CODE_307 + "":
                                mView.showShortToast(R.string.register_verification_code_empty_error);
                                break;
                            case HttpCodeGlobal.RES_CODE_402 + "":
                                mView.showShortToast(R.string.register_verification_code_error);
                                break;
                            case HttpCodeGlobal.RES_CODE_403 + "":
                                mView.showShortToast(R.string.register_phone_has_registered_error);
                                break;
                            case HttpCodeGlobal.RES_CODE_303 + "":
                                Toast.makeText(mActivity, R.string.register_data_check_error, Toast.LENGTH_SHORT).show();
                                break;
                            case HttpCodeGlobal.RES_CODE_441 + "":
                                mView.showShortToast(R.string.register_no_get_verification_code_error);
                                break;
                            case HttpCodeGlobal.RES_CODE_423 + "":
                                mView.showShortToast(R.string.register_account_has_registered_error);
                                break;
                            default:
                                mView.showNetError(bean.getCode(), bean.getDesc());
                                break;
                        }
                    }

                    @Override
                    public void onFinish() {
                        if (null == mView) {
                            return;
                        }
                        mView.setProcessBarVisibility(View.GONE);
                    }
                });
    }


    /**
     * 判断账号是否是中文
     *
     * @param str 用户输入的账号
     * @return 是否是中文
     */
    private boolean isChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        //账号只能为英文+数字，不能包含中文
        Matcher m = p.matcher(str);
        return m.find();
    }
}
